﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Bank
{
    public partial class AccWork : Form
    {
        public AccWork()
        {
            InitializeComponent();
        }

        //非法输入检验
        private bool IsLegal(string a)
        {
            //字符串为空则直接返回false
            if (a == "")
                return false;
            Regex rx = new Regex("^[0-9]*$");
            return rx.IsMatch(a);
        }

        //新建账户
        private void createBtn_Click(object sender, EventArgs e)
        {
            //非法输入检验
            if (!IsLegal(setPwd.Text))
            {
                MessageBox.Show("非法输入，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //创建账户
            int acc = Login.logForm.getBank().openCreditAccount(int.Parse(setPwd.Text));
            MessageBox.Show(string.Format("新开账户成功，新账户账户号为{0}", acc));
        }

        private void destroyBtn_Click(object sender, EventArgs e)
        {
            //非法输入检验
            if (!IsLegal(name.Text) || !IsLegal(pwd.Text))
            {
                MessageBox.Show("非法输入，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //不可销毁当前登录账户
            if(int.Parse(name.Text) == Login.logForm.getLoginAcc())
            {
                MessageBox.Show("该账户为当前登录账户，不可销户", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //检验目标账户是否为存在
            if (Login.logForm.getBank().findCreditAccount(int.Parse(name.Text)) == null)
            {
                MessageBox.Show("未查找到账户，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //获取、核验密码
            int rPwd = Login.logForm.getBank().findCreditAccount(int.Parse(name.Text)).getPwd();
            if (int.Parse(pwd.Text) != rPwd)
            {
                MessageBox.Show("账户号或密码错误，请重新输入", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else  //密码正确，执行销户
            {
                if (Login.logForm.getBank().closeCreditAccount(int.Parse(name.Text)))
                    MessageBox.Show("销户成功");
                else
                    MessageBox.Show("销户失败");
            }
        }

        private void AccWork_Load(object sender, EventArgs e)
        {
            this.ControlBox = false;
        }

        private void backBtn_Click(object sender, EventArgs e)
        {
            this.Close();
            Login.chooseForm.Show();
        }
    }
}
